package com.feifan.monthhelper.db;

import com.feifan.monthhelper.bao.App;
import com.feifan.monthhelper.bean.db.DayRecordBean;
import com.feifan.monthhelper.bean.db.MenstrualRecordBean;
import com.feifan.monthhelper.bean.db.LivingHabitBean;
import com.feifan.monthhelper.bean.db.LoveProtectBean;
import com.feifan.monthhelper.bean.db.MenstrualBloodBean;
import com.feifan.monthhelper.bean.db.PainBean;
import com.feifan.monthhelper.constant.Constant;

import java.util.Date;
import java.util.List;

public class DBUtls {

    //**************** 习惯 ***************//

    public static LivingHabitBean queryBy(Date date) {
        return DBInstance.getInstance(App.getContext()).getLivingHabitDao().loadByDate(date.getTime()/1000 * 1000);
    }

    public static void insert(LivingHabitBean... livingHabitBean) {
        DBInstance.getInstance(App.getContext()).getLivingHabitDao().insert(livingHabitBean);
    }

    public static void update(LivingHabitBean... livingHabitBean) {
        DBInstance.getInstance(App.getContext()).getLivingHabitDao().update(livingHabitBean);
    }

    public static List<LivingHabitBean> getAll() {
        return DBInstance.getInstance(App.getContext()).getLivingHabitDao().getAll();
    }

    //**************** 爱爱保护 ***************//

    public static LoveProtectBean queryLoveProtectBy(Date date) {
        return DBInstance.getInstance(App.getContext()).getLoveProtectDao().loadByDate(date.getTime()/1000 * 1000);
    }

    public static void insertLoveProtect(LoveProtectBean... loveProtectBeans) {
        DBInstance.getInstance(App.getContext()).getLoveProtectDao().insertAll(loveProtectBeans);
    }

    public static void updateLoveProtect(LoveProtectBean loveProtectBeans) {
        LoveProtectBean db_loveProtectBean = DBInstance.getInstance(App.getContext()).getLoveProtectDao().loadByDate(loveProtectBeans.date.getTime()/1000 * 1000);
        if(db_loveProtectBean != null) {
            DBInstance.getInstance(App.getContext()).getLoveProtectDao().update(loveProtectBeans);
        } else {
            DBInstance.getInstance(App.getContext()).getLoveProtectDao().insertAll(loveProtectBeans);
        }
    }

    //**************** 痛经痛感程度 ***************//

    public static PainBean queryPainFeelBy(Date date) {
        return DBInstance.getInstance(App.getContext()).getPainDao().loadByDate(date.getTime()/1000 * 1000);
    }

    public static void updatePainFeel(PainBean painBean) {
        PainBean db_loveProtectBean = DBInstance.getInstance(App.getContext()).getPainDao().loadByDate(painBean.date.getTime()/1000 * 1000);
        if(db_loveProtectBean != null) {
            DBInstance.getInstance(App.getContext()).getPainDao().update(painBean);
        } else {
            DBInstance.getInstance(App.getContext()).getPainDao().insertAll(painBean);
        }
    }

    //**************** 月经流量程度 ***************//

    public static MenstrualBloodBean queryDayRecordBy(Date date) {
        return DBInstance.getInstance(App.getContext()).getMenstrualBloodDao().loadByDate(date.getTime()/1000 * 1000);
    }

    public static void updateMenstrualBlood(MenstrualBloodBean menstrualBloodBean) {
        MenstrualBloodBean db_loveProtectBean = DBInstance.getInstance(App.getContext()).getMenstrualBloodDao().loadByDate(menstrualBloodBean.date.getTime()/1000 * 1000);
        if(db_loveProtectBean != null) {
            DBInstance.getInstance(App.getContext()).getMenstrualBloodDao().update(menstrualBloodBean);
        } else {
            DBInstance.getInstance(App.getContext()).getMenstrualBloodDao().insertAll(menstrualBloodBean);
        }
    }

    //**************** 月经日记录信息 ***************//

    public static void insertDayRecord(DayRecordBean dayRecordBean) {
        DBInstance.getInstance(App.getContext()).getDayRecordDao().insert(dayRecordBean);
    }

    public static void insertDayRecords(List<DayRecordBean> dayRecordBeans) {
        DBInstance.getInstance(App.getContext()).getDayRecordDao().insertAll(dayRecordBeans);
    }

    public static List<DayRecordBean> queryDayRecordBy(Date startDate, Date endDate) {
        return DBInstance.getInstance(App.getContext()).getDayRecordDao().loadBy(startDate.getTime()/1000 * 1000, endDate.getTime()/1000 * 1000);
    }

    public static List<DayRecordBean> queryDayRecordFrom(Date startDate) {
        return DBInstance.getInstance(App.getContext()).getDayRecordDao().loadFrom(startDate.getTime());
    }

    public static List<DayRecordBean> getAllDayRecord() {
        return DBInstance.getInstance(App.getContext()).getDayRecordDao().getAll();
    }

    public static List<DayRecordBean> getAllMenstrualDay() {
        return DBInstance.getInstance(App.getContext()).getDayRecordDao().getAllMenstrualDay(Constant.MenstrualState.MENSTRUAL_DAY);
    }

    public static DayRecordBean loadByDate(Date date) {
        return DBInstance.getInstance(App.getContext()).getDayRecordDao().loadByDate(date.getTime());
    }

    public static List<DayRecordBean> loadLoveBy(int page) {
        return DBInstance.getInstance(App.getContext()).getDayRecordDao().loadLoveByPage(page * Constant.PAGE_COUNT, Constant.PAGE_COUNT);
    }

    public static void deleteAllDayRecord(List<DayRecordBean> dayRecordBeans) {
        DBInstance.getInstance(App.getContext()).getDayRecordDao().deleteAll(dayRecordBeans);
    }

    //**************** 月经周期记录信息 ***************//

    public static void insertMenstrualRecord(MenstrualRecordBean menstrualRecordBean) {
        DBInstance.getInstance(App.getContext()).getMenstrualRecordDao().insert(menstrualRecordBean);
    }

    public static void deleteMenstrualRecord(MenstrualRecordBean menstrualRecordBean) {
        DBInstance.getInstance(App.getContext()).getMenstrualRecordDao().delete(menstrualRecordBean);
    }

    public static void updateMenstrualRecord(MenstrualRecordBean menstrualRecordBean) {
        DBInstance.getInstance(App.getContext()).getMenstrualRecordDao().update(menstrualRecordBean);
    }

    public static MenstrualRecordBean queryMenstrualRecordBy(Date date) {
        return DBInstance.getInstance(App.getContext()).getMenstrualRecordDao().loadByDate(date.getTime());
    }

    public static List<MenstrualRecordBean> queryMenstrualRecordsBy(Date start_date, Date end_date) {
        return DBInstance.getInstance(App.getContext()).getMenstrualRecordDao().loadByDate(start_date.getTime(), end_date.getTime());
    }

    public static MenstrualRecordBean queryMenstrualRecordNearBy(Date date) {
        //86400000刚好是1天的毫秒数
        return DBInstance.getInstance(App.getContext()).getMenstrualRecordDao().loadNearByDate(date.getTime() + 86400000, date.getTime() - 86400000);
    }

    public static List<MenstrualRecordBean> queryAllMenstrualRecord() {
        return DBInstance.getInstance(App.getContext()).getMenstrualRecordDao().getAll();
    }

    public static void deleteAllMenstrualRecord(List<MenstrualRecordBean> menstrualRecordBeans) {
        DBInstance.getInstance(App.getContext()).getMenstrualRecordDao().deleteAll(menstrualRecordBeans);
    }

}
